app.controller('tenantCtrl',['$scope','$rootScope','$http','cookies','myHttp','listSve',function($scope,$rootScope,$http,cookies,myHttp,listSve){
	
	//$rootScope.userName = cookies.getCookie('userName'); //获取cookie用户名
	$rootScope.navIndex = 2;
	
	angular.extend($scope,{
		
		//初始化
		init: function(){
			
			$scope._roomName = '';  //搜索用户名字
			$scope._roomPhone = '';  //搜索用户电话
			$scope.listData = {};
			$scope.iMax = 20;
			$scope.isFirst = true;
			
			//初始化请求
			listSve.tenantList().list({numPerPage:20}).then(function(data) {
				
				console.log(data);
				$scope.listData = data;
				
				//创建分页
		        $scope.iMax = Math.ceil(data.res.page.recordCount/20);
		        $scope.doPager('pager_1',$scope.iMax,$scope.doChangePage); //参数分别为：容器，分页数，分页执行方法
		        
	        });
			
		},
		
		//生成分页
		doPager: function(box,maxNum,pageFn){
			
			var pagerBox = document.getElementById(box);
				pagerBox.innerHTML = '';
			var pager = new Pager({
					index: 1,
					total: maxNum,
					parent: pagerBox,
					onchange: pageFn
				});
				
		},
		
		//分页执行事件
		doChangePage: function(obj){
			
			if($scope.isFirst){
			
				$scope.isFirst = false;
				
			} else{
				
				var _index = obj.index;
				
				listSve.tenantList().list({pageNum: _index,numPerPage:20}).then(function(data) {
				
					console.log(data);
					$scope.listData = data;
					
		        });
				
				
			}
			
		},
		
		//搜索
		search: function(){
			
			$scope.isFirst = true;
			
			listSve.tenantList().list({name: $scope._roomName,mobile: $scope._roomPhone,numPerPage:20}).then(function(data) {
				
				console.log(data);
				$scope.listData = data;
				
				//创建分页
		        $scope.iMax = Math.ceil(data.res.page.recordCount/20);
		        $scope.doPager('pager_1',$scope.iMax,$scope.doChangePage); //参数分别为：容器，分页数，分页执行方法
		        
	        });
			
		}
		
	}).init();
	
}])
